package chances.wechat.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import chances.wechat.entity.ScheduleOrder;
import chances.wechat.resp.BaseResponse;
import chances.wechat.resp.ResultResponse;
import chances.wechat.resp.ResultSetResponse;
import chances.wechat.service.ScheduleOrderService;
import chances.wechat.utils.RequestUtils;

@RestController
@RequestMapping(path = "/sub")
public class ScheduleOrderController extends UserAbstractController {

    private static Logger logger = LoggerFactory.getLogger("contentData");
    
    @Autowired
    private ScheduleOrderService interactionService;

    /**
     * 添加预约
     * 
     * @param model
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/add")
    public BaseResponse add(ModelMap model, HttpServletRequest request,
            HttpServletResponse response) {
        BaseResponse result = new BaseResponse();

        ScheduleOrder userSub = this.createUsersubscr(request);
        String userId = request.getParameter("userId");
        String stbNo = request.getParameter("stbNo");
        String prog = request.getParameter("prog");
        userSub.setUserId(userId);
        userSub.setStbNo(stbNo);
        userSub.setExecStatus(ScheduleOrder.EXEC_STATUS_NO);
        userSub.setProg(prog);
        try {
            this.interactionService.add(userSub);
            result.setStatus(SUCCESS);
        } catch (Throwable ex) {
            result.setStatus(DB_ERROR);
            result.setMessage(ex.getMessage());
        }
        return result;
    }

    /**
     * 查询预约数据
     * 
     * @param model
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/list")
    public ResultSetResponse<ScheduleOrder> list(@RequestParam String userId,
            @RequestParam String stbNo) {
        ResultSetResponse<ScheduleOrder> result = new ResultSetResponse<ScheduleOrder>();
        List<ScheduleOrder> subscriptions = new ArrayList<ScheduleOrder>();
        try {
            subscriptions = this.interactionService.listScheduleOrder(userId,
                    stbNo);
            result.setResultSet(subscriptions);
            result.setStatus(SUCCESS);
        } catch (Throwable ex) {
            result.setStatus(DB_ERROR);
            result.setMessage(ex.getMessage());
        }
        result.setResultSet(subscriptions);
        return result;
    }

    /**
     * 批量删除预约内容
     * 
     * @param model
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/del")
    public ResultResponse<String> dels(@RequestParam String userId,
            @RequestParam String codes, @RequestParam String stbNo) {
        ResultResponse<String> result = new ResultResponse<String>();
        try {
            if (!StringUtils.isNotEmpty(codes)) {
                result.setCode(VALID_PARAM);
                result.setMessage("codes is empty");
            } else {
                String[] codeArr = codes.split(",");
                this.interactionService.deleteSub(userId, codeArr, stbNo);
                result.setStatus(SUCCESS);
            }
        } catch (Throwable ex) {
            result.setStatus(DB_ERROR);
            result.setMessage(ex.getMessage());
        }
        return result;
    }

    /**
     * 清空预约
     * 
     * @param model
     * @param request
     * @param response
     * @return
     * @throws IOException
     */
    @RequestMapping(value = "/clear")
    public BaseResponse clearHistory(@RequestParam String userId,
            @RequestParam String stbNo) throws IOException {
        BaseResponse result = new BaseResponse();
        // 参数检查
        if (StringUtils.isBlank(userId)) {
            result.setStatus(VALID_PARAM);
            result.setMessage("userId is empty");
        } else {
            try {
                this.interactionService.clear(userId, stbNo);
            } catch (Throwable ex) {
                logger.error("clear user history error.", ex);
                result.setStatus(DB_ERROR);
                result.setMessage(ex.getMessage());
            }
        }
        return result;
    }

    private ScheduleOrder createUsersubscr(HttpServletRequest request) {
        ScheduleOrder userSub = new ScheduleOrder();
        userSub.setCode(RequestUtils.getParameter(request, "code", ""));
        userSub.setType(RequestUtils.getParameter(request, "type", ""));
        userSub.setTitle(RequestUtils.getParameter(request, "title", ""));
        userSub.setProg(RequestUtils.getParameter(request, "prog", ""));
        userSub.setIcon(RequestUtils.getParameter(request, "icon", ""));
        userSub.setNextDate(RequestUtils.getParameter(request, "nextDate", ""));
        userSub.setEndDate(RequestUtils.getParameter(request, "endDate", ""));
        return userSub;
    }

}
